package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zbkj.common.request.PageParamRequest;
import com.github.pagehelper.PageHelper;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.URLUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zbkj.common.exception.AppException;

import com.zbkj.common.result.CommonResultCode;
import com.zbkj.common.utils.CrmebDateUtil;
import com.zbkj.common.model.agent.CommunityStatRecordOrder;
import com.zbkj.service.dao.CommunityStatRecordOrderDao;
import com.zbkj.service.service.CommunityStatRecordOrderService;

import com.zbkj.common.request.CommunityStatRecordOrderSaveRequest;
import com.zbkj.common.request.CommunityStatRecordOrderSearchRequest;
import com.zbkj.common.response.CommunityStatRecordOrderPageDataResponse;
import com.zbkj.common.response.CommunityStatRecordOrderDetailResponse;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author Caiyc
* @description CommunityStatRecordOrderServiceImpl 接口实现
* @date 2025-07-24
*/
@Service
public class CommunityStatRecordOrderServiceImpl extends ServiceImpl<CommunityStatRecordOrderDao, CommunityStatRecordOrder> implements CommunityStatRecordOrderService {

    @Resource
    private CommunityStatRecordOrderDao dao;

    @Override
    public Boolean create(CommunityStatRecordOrderSaveRequest communityStatRecordOrderSaveRequest) {
        CommunityStatRecordOrder communityStatRecordOrder = new CommunityStatRecordOrder();
        BeanUtils.copyProperties(communityStatRecordOrderSaveRequest, communityStatRecordOrder, "id");
        boolean save = save(communityStatRecordOrder);
        if (!save) {
            throw new AppException(CommonResultCode.VALIDATE_FAILED, "添加失败");
        }
        return save;
    }

    /**
     * 分页查询小区分红统计排名订单表
     *
     * @param request          请求参数
     * @param pageParamRequest 分页类参数
     * @return List<CommunityStatRecordOrder>
     * @author Caiyc
     * @since 2025-07-24
     */
    @Override
    public List<CommunityStatRecordOrderPageDataResponse> getList(CommunityStatRecordOrderSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        //带 CommunityStatRecordOrder 类的多条件查询
        LambdaQueryWrapper<CommunityStatRecordOrder> lambdaQueryWrapper = Wrappers.lambdaQuery();
        // type 类型不能为空 bean中已经限制
        /*
        lambdaQueryWrapper.eq(CommunityStatRecordOrder::getType, request.getType());

        if (ObjectUtil.isNotEmpty(request.getId()) && request.getId() > 0) {
            lambdaQueryWrapper.eq(CommunityStatRecordOrder::getId, request.getId());
        }
        if (ObjectUtil.isNotEmpty(request.getName())) {
            lambdaQueryWrapper.like(CommunityStatRecordOrder::getName, URLUtil.decode(request.getName()));
        }

        if (ObjectUtil.isNotEmpty(request.getStatus())) {
            lambdaQueryWrapper.eq(CommunityStatRecordOrder::getStatus, request.getStatus());
        }
        if (ObjectUtil.isNotEmpty(request.getMethod())) {
            lambdaQueryWrapper.eq(CommunityStatRecordOrder::getMethod, request.getMethod());
        }*/

        // 时间区间
        //if (ObjectUtil.isNotEmpty(request.getStarttime()) && ObjectUtil.isNotEmpty(request.getEndtime())) {
        //    lambdaQueryWrapper.between(CommunityStatRecordOrder::getCreatetime, request.getStarttime(), request.getEndtime());
        //}
        // * -1 已结束 结束时间小于当前时间
        // * 1 进行中 结束时间大于当前时间 && 开始时间小于等于当前时间
        // * 0 未开始 开始时间大于当前时间
        /*
        if (ObjectUtil.isNotEmpty(request.getRunningStatus())) {
            switch (request.getRunningStatus()) {
                case -1:
                    lambdaQueryWrapper.lt(CommunityStatRecordOrder::getEndtime, currentDate);
                    break;
                case 0:
                    lambdaQueryWrapper.gt(CommunityStatRecordOrder::getStarttime, currentDate);
                    break;
                case 1:
                    lambdaQueryWrapper.ge(CommunityStatRecordOrder::getEndtime, currentDate).le(CommunityStatRecordOrder::getStarttime, currentDate);
                    break;
            }
        }*/

        lambdaQueryWrapper.orderByDesc(CommunityStatRecordOrder::getCreateTime);
        List<CommunityStatRecordOrder> communityStatRecordOrders = dao.selectList(lambdaQueryWrapper);
        // 添加活动状态
        List<CommunityStatRecordOrderPageDataResponse> communityStatRecordOrderResponses = new ArrayList<>();

        communityStatRecordOrders.stream().forEach(x->{
            CommunityStatRecordOrderPageDataResponse communityStatRecordOrderResponse = new CommunityStatRecordOrderPageDataResponse();
            BeanUtils.copyProperties(x, communityStatRecordOrderResponse);
            //communityStatRecordOrderResponse.setRunningStatus(x.getStarttime(), x.getEndtime(), currentDate);
            communityStatRecordOrderResponses.add(communityStatRecordOrderResponse);
        });
        return communityStatRecordOrderResponses;
    }

    /**
    * 修改小区分红统计排名订单表
    * @param request
    * @return
    */
    @Override
    public boolean update(CommunityStatRecordOrderSaveRequest request) {
        if (ObjectUtil.isNull(request.getId()) || request.getId() < 1) {
            throw new AppException(CommonResultCode.VALIDATE_FAILED, "请传入小区分红统计排名订单表ID");
        }
        getByIdException(request.getId());
        CommunityStatRecordOrder communityStatRecordOrder = new CommunityStatRecordOrder();
        BeanUtils.copyProperties(request, communityStatRecordOrder);
        return updateById(communityStatRecordOrder);
    }

    private CommunityStatRecordOrder getByIdException(Integer id) {
        CommunityStatRecordOrder communityStatRecordOrder = getById(id);
        if (ObjectUtil.isNull(communityStatRecordOrder)) {
            throw new AppException("小区分红统计排名订单表不存在");
        }
        if (communityStatRecordOrder.getIsDel()) {
            throw new AppException("小区分红统计排名订单表不存在");
        }
        return communityStatRecordOrder;
    }

    /**
    * 批量删除
    * @param idList
    * @return
    */
    @Override
    public boolean batchDelete(List<Integer> idList) {
    LambdaUpdateWrapper<CommunityStatRecordOrder> luw = Wrappers.lambdaUpdate();
        luw.set(CommunityStatRecordOrder::getIsDel, true);
        luw.in(CommunityStatRecordOrder::getId, idList);
        boolean update = update(luw);
        if (!update) {
            throw new AppException(CommonResultCode.ERROR.setMessage("批量删除小区分红统计排名订单表失败"));
        }
        return true;
    }

    /**
    * 查询详情
    * @param id
    * @return
    */
    @Override
    public CommunityStatRecordOrderDetailResponse getDetails(Integer id) {
        LambdaQueryWrapper<CommunityStatRecordOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(CommunityStatRecordOrder::getIsDel, 0);
        lqw.last("limit 1");
        CommunityStatRecordOrder communityStatRecordOrder = this.getOne(lqw);
        if (ObjectUtil.isNotNull(communityStatRecordOrder)) {
            CommunityStatRecordOrderDetailResponse response = new CommunityStatRecordOrderDetailResponse();
            BeanUtils.copyProperties(communityStatRecordOrder, response);
            return response;
        }
        return null;
    }

}

